package com.tz.springmvc.sysmanage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tz.springmvc.sysmanage.dto.UserDto;
import com.tz.springmvc.sysmanage.entity.User;
import com.tz.springmvc.sysmanage.service.IUserService;
 
/**
 * 用注解的方式开发用户的增删改查功能
 * @author Administrator
 *
 */
@Controller
public class UserControllerRestful{
 	@Autowired
	private IUserService userService;
	
 	/**
 	 * restful风格的用户列表
 	 * @return
 	 */
	@RequestMapping(value="/users",method = RequestMethod.GET)
	public @ResponseBody List<User> getUserListRestful(){
		List<User> userList = userService.getUserList(new User());
		return userList;
	}
	
	//进入用户管理页面
	@RequestMapping("/gotoUser")
	public String gotoUser(){
		return "sysmanage/user/userMain";		
	}
	
	/**
	 * restful风格的用户信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/user/{id}" ,method = RequestMethod.GET)
	public @ResponseBody User getUserByIdRestful(@PathVariable Long id){
		User user = new User();
		user = userService.getUserById(id);
		return user;
	}
	
 
	/**
	 * restful风格的新增
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/user" ,method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> userAddRestful(@RequestBody User user){		
		 Map<String,Object> resultMap = new HashMap<String,Object>();		 
		 if(userService.addUser(user)){
				resultMap.put("result", "新增成功") ; 
			}else{
				resultMap.put("result", "新增用户失败");  
			} 		  
		return resultMap;
	}
	 	
	/**
	 * restful风格的修改
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/user" ,method = RequestMethod.PUT)
	public @ResponseBody Map<String,Object> userUpdateRestful(@RequestBody User user){		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		 
	 	if(userService.updateUser(user)){
			resultMap.put("result", "修改用户成功") ; 
		}else{
			resultMap.put("result", "修改用户失败");  
		}	
	  
		return resultMap;
	}
	
	 
	
	/**
	 * restful风格的删除
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/user/{userId}" ,method = RequestMethod.DELETE)
	public @ResponseBody Map<String,Object> userDelRestful(@PathVariable Long userId){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		if(userId!=null){
			if(userService.delUser(userId)){
				resultMap.put("result", "删除用户成功") ; 
			}else{
				resultMap.put("result", "删除用户失败");  
			}
		}		 
		return resultMap;
	}
  
	 
}
